#-*- coding:utf-8 -*-
from selenium import webdriver
import time
from KeywordDriven.common.logger import FrameLog


class Library:
    logging=FrameLog().getLogger()
    def open_brower(self,browser):
        browser=browser.capitalize()
        #获取不同浏览器
        try:
            self.driver=getattr(webdriver,browser)()
            self.logging.info(f"打开{browser}浏览器")
        except:
            self.logging.info(f"无法打开{browser}浏览器")

    def sleep_wait(self,s):
        time.sleep(s)

    def load_url(self,url):
        try:
            self.driver.get(url)
            self.logging.info(f"加载项目弟子：{url}")
        except:
            self.logging.error(f"加载项目弟子：{url}，加载失败")

    # 获取元素
    def locator(self,type,express):
        try:
            el=self.driver.find_element(type, express)
            self.logging.info(f"定位元素{type,express},定位成功")
            return el
        except:
            self.logging.error(f"定位元素{type,express},定位失败")

    # 输入
    def input(self,type,express,value):
        try:
            # 获取元素
            el=self.locator(type,express)
            # 输入
            el.send_keys(value)
            self.logging.info(f"操作元素{type, express}输入{value}成功")
        except:
            self.logging.error(f"操作元素{type,express}输入{value}异常")

    # 点击
    def click(self,type,express):
        try:
            # 获取元素
            el = self.locator(type, express)
            # 输入
            el.click()
            self.logging.info(f"操作元素{type, express}点击")
        except:
            self.logging.error(f"操作元素{type,express}点击异常")


    # 可以调用当前类下面送哟的方法

    def run(self,keyword,*args):
        # 基于关键字的反射
        getattr(self,keyword)(*args)

    # 验证某个元素的文本内容 type断言的类型  express实际结果
    def asserText(self,type,express,expect):
        pass
        # el=self.locator(type,express)
        # fact=el.text
        # print(fact)
        # if fact != expect:
        #     raise Exception(f"assertText：{fact} !={express},断言失败")